﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SimpleRcon.Windows;
using SimpleRcon.Tabs;

namespace SimpleRcon
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Main : Window
    {
        // These are temp, should find a other way :D
        private string nextTabServerAddress { get; set; }
        private string nextTabPassword { get; set; }

        // Temp for bookmarks
        private bool nextTabIsBookmark { get; set; }
        private Bookmark nextTabBookmark { get; set; }

        public Main()
        {
            InitializeComponent();
            TabControler.TabItemAdded += new EventHandler<Wpf.Controls.TabItemEventArgs>(TabControler_TabItemAdded);
            TabControler.TabItemAdding += new EventHandler<System.ComponentModel.CancelEventArgs>(TabControler_TabItemAdding);

            // Load Bookmarks
            LoadBookmarks();
        }

        #region Tabs

        void TabControler_TabItemAdding(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!nextTabIsBookmark)
            {
                // If it aint a bookmark, request server info
                ServerInfoWindow requestInfo = new ServerInfoWindow();
                requestInfo.Owner = this;

                // Show
                if (requestInfo.ShowDialog() == true)
                {
                    nextTabServerAddress = requestInfo.ServerAddress;
                    nextTabPassword = requestInfo.Password;
                }
                else
                {
                    // No info, cancel tab
                    e.Cancel = true;
                }
            }
        }

        void TabControler_TabItemAdded(object sender, Wpf.Controls.TabItemEventArgs e)
        {
            if (!nextTabIsBookmark)
            {
                // if it aint a bookmark
                e.TabItem.Content = new Tabs.RconTab(TabControler, e.TabItem, nextTabServerAddress, nextTabPassword);

                // Clean up values
                nextTabServerAddress = null;
                nextTabPassword = null;
            }
            else
            {
                // Is a bookmark
                e.TabItem.Content = new Tabs.RconTab(TabControler, e.TabItem, nextTabBookmark.Address, nextTabBookmark.Password);

                // Clean up the values
                nextTabIsBookmark = false;
                nextTabBookmark = null;
            }

            e.TabItem.ContextMenu = new ContextMenu();

            MenuItem menuItemClose = new MenuItem();
            menuItemClose.Header = "Close this";
            menuItemClose.Tag = e.TabItem;
            menuItemClose.Click += new RoutedEventHandler(menuItemClose_Click);
            e.TabItem.ContextMenu.Items.Add(menuItemClose);

            MenuItem menuItemCloseOthers = new MenuItem();
            menuItemCloseOthers.Header = "Close all others";
            menuItemCloseOthers.Tag = e.TabItem;
            menuItemCloseOthers.Click += new RoutedEventHandler(menuItemCloseOthers_Click);
            e.TabItem.ContextMenu.Items.Add(menuItemCloseOthers);

        }

        #endregion

        #region TabItem Context Menu

        void menuItemClose_Click(object sender, RoutedEventArgs e)
        {
            TabControler.Items.Remove(((MenuItem)sender).Tag);
        }

        void menuItemCloseOthers_Click(object sender, RoutedEventArgs e)
        {
            TabItem[] items = TabControler.Items.Cast<TabItem>().ToArray();
            foreach (var item in items)
            {
                if (item != ((MenuItem)sender).Tag)
                {
                    TabControler.Items.Remove(item);
                }
            }
        }

        #endregion

        #region Application Context Menu

        #region Files

        private void MenuItemClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Commands

        private void MenuItemChangeMap_Click(object sender, RoutedEventArgs e)
        {
            if (TabControler.SelectedContent is RconTab)
            {
                if (((RconTab)TabControler.SelectedContent).rcon.Connected)
                {
                    ChangeMapWindow window = new ChangeMapWindow(((RconTab)TabControler.SelectedContent).rcon);
                    window.Owner = this;
                    window.ShowDialog();
                }
            }
        }

        private void MenuItemPlayers_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("No done yet");
        }

        #endregion

        #region Bookmarks

        private void LoadBookmarks()
        {
            menuitem_Bookmarks.Items.Clear();

            MenuItem manageBookmarks = new MenuItem();
            manageBookmarks.Header = "Manage";
            manageBookmarks.Click += new RoutedEventHandler(manageBookmarks_Click);
            menuitem_Bookmarks.Items.Add(manageBookmarks);

            // Only add the seperator if there is bookmarks, else it looks stupid.
            if (BookmarkCollection.Items.Count() > 0)
            {
                Separator sep = new Separator();
                menuitem_Bookmarks.Items.Add(sep);
            }

            foreach (Bookmark bookmark in BookmarkCollection.Items)
            {
                MenuItem item = new MenuItem();
                item.Header = bookmark.Name;
                item.Tag = bookmark;
                item.Click += MenuItemBookmark_Click;
                menuitem_Bookmarks.Items.Add(item);
            }
        }

        void manageBookmarks_Click(object sender, RoutedEventArgs e)
        {
            ManageBookmarksWindow bookmarksWindow = new ManageBookmarksWindow();
            bookmarksWindow.Owner = this;
            bookmarksWindow.ShowDialog();

            // Reload bookmarks
            LoadBookmarks();
        }

        private void MenuItemBookmark_Click(object sender, RoutedEventArgs e)
        {
            // Set values
            nextTabIsBookmark = true;
            nextTabBookmark = (Bookmark)((MenuItem)sender).Tag;

            // Add tab
            TabControler.AddTabItem();
        }

        #endregion

        #region About

        private void AboutMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Windows.AboutWindow about = new SimpleRcon.Windows.AboutWindow();
            about.ShowDialog();
        }

        #endregion

        #endregion

    }
}
